package net.bolue.web.mvc;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.bolue.console.data.UpdateInfo;
import net.bolue.console.data.model.UpdateInfoModel;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.web.entry.FormTable;
import net.bolue.web.util.FormUtil;

/**
 * 初始化更新页面
 * @author Administrator
 *
 */
public class InitUpdateAction extends AbstractAction{

	@Override
	public ActionResult perform(HttpServletRequest request, HttpServletResponse response) throws BolueException{
//		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
//		String updateId = RequestUtil.getParamValue(request, RepConstants.REP_UPDATEID_KEY);
//		if(subSys==null || subSys.trim().length()==0 || updateId==null || updateId.trim().length()==0){
//			throw new BolueException(1, "修改URL中必须提供subSys及updateID参数值!");
//		}
//		Map<String,Object> whereMap = new HashMap<String,Object>();
//		whereMap.put("UPDATEID", updateId);
//		whereMap.put("SUBSYS", subSys);
//		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
//		UpdRes updRes = dbOpUtil.getOne(UpdRes.class, "st_updres", null, whereMap);
//		if(updRes==null){
//			throw new BolueException(10, "没有找到subSys="+subSys+",updateID="+updateId+"的更新记录所用资源！");
//		}
		
		ActionResult actionResult = new ActionResult();
		UpdateInfoModel updateInfoModel = new UpdateInfoModel(request);
		UpdateInfo updInfo = updateInfoModel.getUpdInfo();
		FormTable formTable = FormUtil.getFTForUpd(updInfo);
		Map<String,Object> pageMap = new HashMap<String,Object>();
		pageMap.put("subSys", updInfo.getSubSys());
		pageMap.put("updateId", updInfo.getUpdateId());
		pageMap.put("formTable", formTable);
		actionResult.setData(pageMap);
		actionResult.setResult(true);
		return actionResult;
	}
	
	/*
	public void putEditData(Map<String,Object> returnMap,UpdRes updRes,HttpServletRequest request)throws BolueException{
		Map<String,Object> whereMap = new HashMap<String,Object>();
		String subSys = updRes.getSubSys();
		String tableName = updRes.getResObj();
		String keyColInfoSql = "SELECT * FROM st_colinfo where SUBSYS='"+subSys+"' AND TBLNAME='"+tableName+"' ORDER BY DISPNO";
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		List<ColInfo> keyColInfoList = dbOpUtil.query(ColInfo.class,keyColInfoSql, null);
		if(keyColInfoList!=null){
			for(ColInfo colInfo : keyColInfoList){
				String keyName = colInfo.getColName();
				String keyValue = RequestUtil.getParamValue(request, "col_"+keyName);
				if(colInfo.getIsPkey()==1&&(keyValue==null||"".equals(keyValue))){
					throw new BolueException(1, "在request中，必须提供参数"+"col_"+keyName+"!");
				}
				if(colInfo.getIsPkey()==1||(keyValue!=null&&!"".equals(keyValue))){
					Object parseValue = RequestUtil.parseValue(colInfo.getJType(), keyValue);
					whereMap.put(keyName, parseValue);
				}
			}
		}
		DbOpUtil dbOpUtil2 = new DbOpUtil(subSys);
		Map<String,Object> dataMap = dbOpUtil2.getOne(tableName, null, whereMap);
		returnMap.put("dataMap", dataMap);
	}
	
	public void putElemTag(Map<String,Object> returnMap,UpdRes updRes,HttpServletRequest request)throws BolueException{
		List<FormElemTag> elemTagList = new ArrayList<FormElemTag>();
		Map<String,List<FormDataOption>> dicsMap = new HashMap<String,List<FormDataOption>>();
		
		String subSys = updRes.getSubSys();
		String tableName = updRes.getResObj();
		String updCols = updRes.getUpdCols();
		updCols = ","+updCols+",";
		String dispCols = updRes.getDispCols();
		dispCols = ","+dispCols+",";
		String colInfoSql = "SELECT * FROM st_colinfo where SUBSYS='"+subSys+"' AND TBLNAME='"+tableName+"' ORDER BY DISPNO";
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		List<ColInfo> colInfoList = dbOpUtil.query(ColInfo.class,colInfoSql, null);
		if(colInfoList!=null){
			for(ColInfo colInfo : colInfoList){
				String colName = colInfo.getColName();
				FormElemTag formElemTag = null;
				String reg = ".*,"+colName+",.*";
				if(!updCols.matches(reg)&&!dispCols.matches(reg)){
					continue;
				}
				formElemTag = TagUtil.colToField(colInfo);
				if(dispCols.matches(reg)){
					formElemTag.setTagType(-1);
				}
				elemTagList.add(formElemTag);
				Integer  dicGrpId = 0;
				if(colInfo.getDicGrpid()!=null){
					dicGrpId = colInfo.getDicGrpid();
				}
				List<FormDataOption> optionList = DictUtil.getOptionOfAllDic(colInfo.getDicKind(), colInfo.getDicSubSys(), dicGrpId);
				if(optionList!=null&&optionList.size()>0){
					dicsMap.put(colInfo.getDicKind()+"_"+colInfo.getDicGrpid(), optionList);
				}
			}
		}
		
		returnMap.put("elemTagList", elemTagList);
		returnMap.put("dicsMap", dicsMap);
	}

	public List<UpdTabPage> getTabPage(String subSys,String updateId)throws BolueException{
		Map<String,Object> whereMap = new HashMap<String,Object>();
		whereMap.put("UPDATEID", updateId);
		whereMap.put("SUBSYS", subSys);
		Map<String,String> orderMap = new HashMap<String,String>();
		orderMap.put("PAGENO", "ASC");
		DbOpUtil dbOpUtil = new DbOpUtil(RepConstants.SYSMNG_SUBSYS_KEY);
		List<UpdTabPage> list = dbOpUtil.query(UpdTabPage.class, "st_updtabpage", null, whereMap, orderMap);
		return list;
	}
	*/
}
